package com.youlai.boot.system.controller;

import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.system.service.LpsWlService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.LpsWlForm;
import com.youlai.boot.system.model.query.LpsWlQuery;
import com.youlai.boot.system.model.vo.LpsWlVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 物料前端控制层
 *
 * @author lwb
 * @since 2025-07-16 21:52
 */
@Tag(name = "物料接口")
@RestController
@RequestMapping("/api/v1/lpsWls")
@RequiredArgsConstructor
public class LpsWlController  {

    private final LpsWlService lpsWlService;

    @Operation(summary = "物料分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:lpsWl:query')")
    public PageResult<LpsWlVO> getLpsWlPage(LpsWlQuery queryParams ) {
        IPage<LpsWlVO> result = lpsWlService.getLpsWlPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增物料")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:lpsWl:add')")
    @Log(value = "新增物料", module = LogModuleEnum.OTHER)
    public Result<Void> saveLpsWl(@RequestBody @Valid LpsWlForm formData ) {
        boolean result = lpsWlService.saveLpsWl(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取物料表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:lpsWl:edit')")
    public Result<LpsWlForm> getLpsWlForm(
        @Parameter(description = "物料ID") @PathVariable Long id
    ) {
        LpsWlForm formData = lpsWlService.getLpsWlFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改物料")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:lpsWl:edit')")
    @Log(value = "修改物料", module = LogModuleEnum.OTHER)
    public Result<Void> updateLpsWl(
            @Parameter(description = "物料ID") @PathVariable Long id,
            @RequestBody @Validated LpsWlForm formData
    ) {
        boolean result = lpsWlService.updateLpsWl(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除物料")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:lpsWl:delete')")
    @Log(value = "删除物料", module = LogModuleEnum.OTHER)
    public Result<Void> deleteLpsWls(
        @Parameter(description = "物料ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lpsWlService.deleteLpsWls(ids);
        return Result.judge(result);
    }
}
